package com.vhall.autotest.api_test.service.cloudwise;

import java.util.HashMap;
import java.util.Map;

import org.junit.Assert;

import com.alibaba.fastjson.JSONObject;
import com.vhall.autotest.api_test.common.Constant;
import com.vhall.autotest.api_test.common.Http;
import com.vhall.autotest.api_test.common.Http_new;
import com.vhall.autotest.api_test.common.HttpUtil;
import com.vhall.autotest.api_test.service.code.ICode;
import com.vhall.autotest.api_test.service.code.IWebinarCode;

public class GetToken implements IWebinarCode{

	private static final String oathUrl = Constant.apiDomain + "/v2/oauth/token.json";
	/*
	 * 获取token
	 * 2016-5-25
	 */
	public static String getToken_success(String grant_type, String username, String password,
			String client_id, String client_secret) {
		Http http = new Http();
		Map<String, String> param = new HashMap<String, String>();
		param.put("grant_type", grant_type);
		param.put("username", username);
		param.put("password", password);
		param.put("client_id", client_id);
		param.put("client_secret", client_secret);
		System.out.println("param:" + param);
		try {
			Map<String, String> result = http.post(oathUrl, param);
			String statusCode = result.get("statusCode");
			String resultText = result.get("resultText");
			System.out.println("statusCode:" + statusCode);
			Assert.assertNotNull(statusCode);
			Assert.assertNotNull(resultText);
			Assert.assertTrue(String.valueOf(ICode.statusCodeSuccess).equals(statusCode));
			JSONObject obj = JSONObject.parseObject(resultText);
			System.out.println(obj);
			Assert.assertTrue(obj.containsKey("access_token"));
			String access_token = obj.getString("access_token");
			System.out.println(access_token);
			return access_token;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) {
		GetToken.getToken_success("password", "admin@jiankongbao.com", "26ea57518a98bf2d89b0881502c605d6", "162", "fd5d03a1931e81d682c7c3ed16d695b6b47c0673");
	}
	
	
	
}

